<?php
namespace Admin\Model;
use Think\Model;

/**
 * 菜单设置模块
 * @author 
 *        
 */
class MenuModel extends Model
{
   protected $tableName = 'auth_rule';
   /**
    * 删除菜单
    * @param string $ids
    */
   public function menu_del($ids=''){
        $ids = isset($ids) ? $ids : false;
        if (!$ids) {
            $this->ajaxReturn('请勾选删除菜单！');
        }
        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
            if (!$ids) {
                $this->ajaxReturn('参数错误！');
                $uids = implode(',', $uids);
            }
        }

        //判断该栏目是否有子栏目
        $son_ids=$this->getChildsId(M('auth_rule')->order('o asc')->select(),$ids);
        if(!empty($son_ids)){//有子栏目，不允许删除
            $this->ajaxReturn('有子栏目，不允许删除');
        }

        $map['id'] = array('in', $ids);
        if (M('auth_rule')->where($map)->delete()) {

            addlog('删除菜单ID：' . $ids);
            return true;
        } else {
            return false;
        }
   }
   
   /**
    * 
    * @param string $info
    */
   public function menu_update($info=''){
       $id = $info['id'];
       $data['pid'] = $info['pid'];
       $data['title'] = $info['title'];
       $data['name'] = $info['name'];
       $data['icon'] = 'menu-icon '.$info['icon'];
       $data['islink'] = !empty($info['islink']) ? $info['islink'] : 0;
       $data['status'] = 1;
       $data['o'] = $info['o'];
       $data['tips'] = $info['tips'];
       if ($id) {
           M('auth_rule')->data($data)->where("id=".$id)->save();
           addlog('编辑菜单，ID：' . $id);
       } else {
           M('auth_rule')->data($data)->add();
           addlog('新增菜单，名称：' . $data['title']);
       }
       
       return true;
   }
   
   public function menu(){
       $option = M('auth_rule')->order('o ASC')->select();
       $option = getMenu($option);
       return $option;
   }
   

   /**
    * 传递一个父级分类ID返回所有子分类ID
    * @param unknown $cate
    * @param unknown $pid
    */
   public function getChildsId ($cate, $pid) {
       $arr = array();
       foreach ($cate as $v) {
           if ($v['pid'] == $pid) {
               $arr[] = $v['id'];
               $arr = array_merge($arr, $this->getChildsId($cate, $v['id']));
           }
       }
       return $arr;
   }
   
}

?>